//
//  RecordingHudView.m
//  ChangChat
//
//  Created by Cocoa on 2019/8/24.
//  Copyright © 2019 yibai. All rights reserved.
//

#import "RecordingHudView.h"
#import <KKPaddingLabel/KKPaddingLabel.h>

@interface RecordingHudView ()

@property (nonatomic, strong) UIImageView *recordingImgView;
@property (nonatomic, strong) UIImageView *cancelImgView;
@property (nonatomic, strong) KKPaddingLabel *tipLabel;
@property (nonatomic, strong) UILabel *countdownLabel;

@end
@implementation RecordingHudView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    self.backgroundColor = UIColor.k_RGBAColor(0,0,0,0.3);
    self.layer.cornerRadius = 5.0f;
    
    self.recordingImgView = [UIImageView new];
    self.cancelImgView = [UIImageView new];
    self.tipLabel = [KKPaddingLabel new];
    UIImage *img1 = UIImage.k_imgName(@"msg_voice_recording1");
    UIImage *img2 = UIImage.k_imgName(@"msg_voice_recording2");
    UIImage *img3 = UIImage.k_imgName(@"msg_voice_recording3");
    self.recordingImgView.animationImages = @[img1,img2,img3];
    self.recordingImgView.animationDuration = 1.0f;
    
    self.cancelImgView.image = UIImage.k_imgName(@"msg_voice_cancel");
    
    self.tipLabel.text = @"手指上滑，取消发送";
    self.tipLabel.k_fontSize(10).k_textColor(UIColor.whiteColor);
    self.tipLabel.padding = UIEdgeInsetsMake(3, 5, 3, 5);
    self.tipLabel.k_cornerRadius(3.0f);
    
    
    self.countdownLabel = [UILabel k_labelWithBoldFontSize:40 textColor:UIColor.whiteColor];
    self.countdownLabel.hidden = YES;
    
    [self addSubview:self.recordingImgView];
    [self addSubview:self.cancelImgView];
    [self addSubview:self.tipLabel];
    [self addSubview:self.countdownLabel];
    
    [self.recordingImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_offset(0);
        make.centerY.mas_offset(-10);
    }];
    [self.cancelImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_offset(-10);
        make.centerX.mas_offset(0);
    }];
    [self.tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_offset(0);
        make.bottom.mas_offset(-5);
    }];
    [self.countdownLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_offset(0);
        make.centerY.mas_offset(-10);
    }];
}

- (void)setStatus:(int)status {
    _status = status;
    
    
    if (status == 0) {
        if (self.countdownLabel.hidden == YES) {
            self.recordingImgView.hidden = NO;
            [self.recordingImgView startAnimating];
            self.cancelImgView.hidden = YES;
        }
        self.tipLabel.text = @"手指上滑，取消发送";
        self.tipLabel.backgroundColor = UIColor.clearColor;
    } else if (status == 1) {
        if (self.countdownLabel.hidden == YES) {
            self.cancelImgView.hidden = NO;
            self.recordingImgView.hidden = YES;
            [self.recordingImgView stopAnimating];
            self.cancelImgView.image = UIImage.k_imgName(@"msg_voice_cancel");
        }
        self.tipLabel.text = @"松开手指，取消发送";
        self.tipLabel.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.3];
    } else if (status == 2) {
        self.recordingImgView.hidden = YES;
        [self.recordingImgView stopAnimating];
        self.cancelImgView.hidden = NO;
        self.cancelImgView.image = UIImage.k_imgName(@"msg_voice_warning");
        self.tipLabel.text = @"说话时间太短";
        self.tipLabel.backgroundColor = UIColor.clearColor;
    }
}

- (void)show {
    [kWindow addSubview:self];
    [self mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(kWindow);
        make.size.mas_equalTo(CGSizeMake(150, 150));
    }];
}
- (void)hide {
    self.countdownLabel.hidden = YES;
    if (self.status == 1) {
        [self.recordingImgView stopAnimating];
    }
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        self.alpha = 1;
    }];
}
- (void)setProgress:(NSInteger)progress {
    if (_progress == progress) {
        return;
    }
    _progress = progress;
    if (progress > 50) {
        
        self.recordingImgView.hidden = YES;
        self.cancelImgView.hidden = YES;
        self.countdownLabel.hidden = NO;
        self.countdownLabel.text = [NSString kk_stringWithInteger:60 - progress + 1];
        
    } else {
        
    }
}

@end
